//
//  ViewController.m
//  UIView和CALayer的区别
//
//  Created by 伍银荣 on 2021/7/9.
//  Copyright © 2021 com.WYR. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()

@property (nonatomic, weak) CALayer *layer;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    //CALayer的基本使用
    UIView *view = [[UIView alloc]initWithFrame:CGRectMake(100, 100, 100, 100)];
    view .backgroundColor = [UIColor redColor];
    //设置layer的阴影
    //设置阴影的偏移量
    view.layer.shadowOffset = CGSizeMake(200, 100);
    view.layer.shadowColor = [UIColor greenColor].CGColor;
    //设置阴影的圆角半径
    view.layer.shadowRadius = 20;
    //Opacity: 不透明  设置阴影的透明度(0-1):数字越小说明月透明,颜色越淡
    view.layer.shadowOpacity = 1;
    
    //设置layer的圆角半径
    view.layer.cornerRadius = 50;
    //裁剪超出layer范围的东西
    view.layer.masksToBounds = YES;  //这句会将显示的阴影也裁剪掉
    
    //设置layer的边框
    view.layer.borderWidth = 10;
    view.layer.borderColor = [UIColor blueColor].CGColor;
    
    //设置layer 的内容(图片)
    view.layer.contents = (__bridge id)([UIImage imageNamed:@"1"].CGImage);
    
    //设置layer 的bounds
    view.layer.bounds = CGRectMake(0, 0, 200, 200);
    //设置view的中心点在(0, 0)的位置,和view.center设置在(0, 0)效果是一样的
    view.layer.position = CGPointMake(200, 200);
    
    [self.view addSubview:view];
    
    //控件的根layer没有隐式动画(根layer就是view下d面一层的layer)
    self.layer = view.layer;
}



-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    //获取触摸对象
    UITouch *t = touches.anyObject;
    //获取当前手指所在的位置
    CGPoint currentPoint = [t locationInView:t.view];
    //让layer跑到手指的位置
    self.layer.position = currentPoint;
}


@end
